package com.gec.system.util;



import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.After;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@RunWith(MockitoJUnitRunner.class)
public class MD5HelperTest {

    private static final Logger log = LoggerFactory.getLogger(MD5HelperTest.class);
    @InjectMocks
    private MDHelper md5Helper;

    @Before
    public void setUp() {
        // Any setup can be done here if needed in future
    }

    @After
    public void tearDown() {
        // Any cleanup can be done here if needed in future
    }

    @Test
    public void encrypt_ValidString_ReturnsMD5Hash() {
        String input = "Hello World";
        String expectedHash = "b10a8db164e0754105b7a99be72e3fe5"; // Expected MD5 for "Hello World"
        String actualHash = MDHelper.encrypt(input);
        Assert.assertEquals(expectedHash, actualHash);
        log.info("解密成功");
    }

    @Test
    public void encrypt_EmptyString_ReturnsMD5Hash() {
        String input = "";
        String expectedHash = "d41d8cd98f00b204e9800998ecf8427e"; // Expected MD5 for an empty string
        String actualHash = MDHelper.encrypt(input);

        Assert.assertEquals(expectedHash, actualHash);
        log.info("解密成功");

    }

    @Test
    public void encrypt_NullString_ThrowsRuntimeException() {
        try {
            MDHelper.encrypt(null);
            Assert.fail("Expected RuntimeException to be thrown");
        } catch (RuntimeException e) {
            Assert.assertTrue(e.getMessage().contains("MD5加密出错"));
        }
    }

    @Test
    public void encrypt_SpecialCharacters_ReturnsMD5Hash() {
        String input = "!@#$%^&*()_+=<>?{}[]|,";
        String expectedHash = "dd738ecd012307a0df311ced32c8c128"; // Expected MD5 for the special characters string
        String actualHash = MDHelper.encrypt(input);

        Assert.assertEquals(expectedHash, actualHash);
    }

    @Test
    public void encrypt_DifferentStrings_ReturnsDifferentHashes() {
        String input1 = "1234";
        String input2 = "1234";
        String hash1 = MDHelper.encrypt(input1);
        String hash2 = MDHelper.encrypt(input2);

        Assert.assertNotEquals(hash1, hash2);
    }

    @Test
    public void encrypt_SameStrings_ReturnsSameHashes() {
        String input = "hello";
        String hash1 = MDHelper.encrypt(input);
        String hash2 = MDHelper.encrypt(input);

        Assert.assertEquals(hash1, hash2);
    }
}
